/*** 统计所有已点过的菜品，返回值 ***/
function totalSelectedDishesTemp(){
	var menu = document.getElementById("menu");
	var menuDiv = menu.getElementsByTagName("div");
	var menuDivNum = menuDiv.length;
	var serialNum = 1;
	var sumDishPrice = 0;
	selectedDishesTemp = ""
	selectedDishesTemp += '<h1>我的菜单</h1>';
	selectedDishesTemp += '<ul class="myOrderList" id="myOrderList">';
	//alert(selectedDishesTemp);
	//alert("menuDiv.length==="+menuDiv.length);
	//alert(selectedDishes);
	
	for (var i = 1 ; i < menuDivNum - 2 ; i++  )
	{	//alert(menuDiv[i].getElementsByTagName("section")[1].getElementsByTagName("dl").length);
		for(var s = 1; s<menuDiv[i].getElementsByTagName("section").length; s++)
		{	//alert("section  dl=="+menuDiv[i].getElementsByTagName("section")[s].getElementsByTagName("dl").length);
			for (var j = 0 ; j < menuDiv[i].getElementsByTagName("section")[s].getElementsByTagName("dl").length ; j++ )
			{	
				var dishLi = menuDiv[i].getElementsByTagName("section")[s].getElementsByTagName("dl")[j];
				//alert(dishLi);
				for (var k = 0 ; k < selectedDishes.length ; k++ )
				{
					//alert(selectedDishes[k] +"--------------"+ dishLi.id.split("_")[1]);
					if ( selectedDishes[k] == dishLi.id.split("_")[1] )
					{	
	//					if ( dishLi.getElementsByTagName("img").length >= 1 && dishLi.getElementsByTagName("img")[0].className != "good" )
	//					{
	//						var start = dishLi.innerHTML.indexOf(">");
	//						var end = dishLi.innerHTML.indexOf("<",start);
	//						var dishName = dishLi.innerHTML.substring(start+1,end);
	//					}
	//					else 
	//					{
	//						var dishName = dishLi.innerHTML.substring(0,dishLi.innerHTML.indexOf("<"));
	//					}
						var dishName = " " + dishLi.getElementsByTagName("summary")[0].innerHTML;
						
						var dishPrice = dishLi.getElementsByTagName("label")[0].innerHTML;
						if ( ( browser.versions.ios == true ) )
						{
							selectedDishesTemp += '<li style="position:relative;left:-40px;">';//针对Iphone
						}
						else{
							selectedDishesTemp += '<li>';
						}
						selectedDishesTemp += '<span class="dish">' + serialNum + dishName + '</span>';
						selectedDishesTemp += '<span class="price">￥<label id="dishPrice_1">' + dishPrice + '</label></span>';
						selectedDishesTemp += '<span class="amount">';
						selectedDishesTemp += '<input type="button" value="x" onclick="delSelectedDishes(this,\''+ dishLi.id.split("_")[1] +'\')" class="delDish" id="delDish">';
						selectedDishesTemp += '</span>';
						selectedDishesTemp += '</li>';
						sumDishPrice += parseInt(dishPrice);
						serialNum++;
					}
				}
			}
		}
	}
	selectedDishesTemp += '<li class="sum">';
	selectedDishesTemp += '预计消费合计 <font color="red"><strong>￥<label id="sumPriceA">' + sumDishPrice + '</label></strong></font> 元 ( 共<label id="sumDishesA">' + (serialNum-1) + '</label>道菜 )';
	selectedDishesTemp += '<br/><label class="info">注：下单之后可调整每道数量</label>';
	selectedDishesTemp += '</li>';
	selectedDishesTemp += '</ul>';
	return selectedDishesTemp;
}
function delSelectedDishes(target,dish){
	target.parentNode.parentNode.style.display="none";
	delDish(dish);
	document.getElementById("sumPriceA").innerHTML = document.getElementById("sumPrice").innerHTML;
	document.getElementById("sumDishesA").innerHTML = document.getElementById("sumDishes").innerHTML;
}

/***删除某个已点菜品***/
function delDish(dishID){
	for ( i = 0 ; i < selectedDishes.length ; i++ ){
		if ( selectedDishes[i] == dishID )
		{
			document.getElementById( "dishes_" + selectedDishes[i] ).style.backgroundColor = 'transparent';				
			document.getElementById( "dishes_" + selectedDishes[i] ).getElementsByTagName("input")[0].checked = false;
			selectedDishes.splice(i,1);
			sum();
		}
	}
}
/***增加某个菜品为已点***/
function addDish(dishID){
	document.getElementById( "dishes_" + dishID ).style.backgroundColor = '#E1E1E1';
	document.getElementById( "dishes_" + dishID ).getElementsByTagName("input")[0].checked = true;
	selectedDishes.push( dishID );
}



var selectedDishes = new Array();//存储已选菜品
var shop = "shop_1";//window.location.search.split("sid=")[1];//从地址栏取餐厅ID( 地址实例：m.cyooy.com/indRestaurantMenu.php?id=186181,186298&sid=3568 )
/************************
**计算合计消费与已点菜品数量
*************************/
function sum(){
		document.getElementById("sumDishes").innerHTML = selectedDishes.length ;
//		 $("#sumDishes").innerHTML = selectedDishes.length ;
		var sumPrice = 0;
		//var sumPriceLabel = document.getElementById("sumPrices");
		for (var i=0 ; i < selectedDishes.length ; i++ )
		{	
			sumPrice += parseFloat(document.getElementById( "dishes_" + selectedDishes[i]).getElementsByTagName("label")[0].innerHTML);
//			sumPrice += parseFloat($("#dishes_" + selectedDishes[i]).attr('label')[0].innerHTML);
		}
//		alert(sumPrice);
		document.getElementById("sumPrice").innerHTML = sumPrice ;
		document.getElementById("sumDishesA").innerHTML = selectedDishes.length ;
//		$("#sumPrice").innerHTML = sumPrice ;
//		$("#sumDishesA").innerHTML = selectedDishes.length ;
		setCookie(shop,selectedDishes,30);//将已点菜品放入以餐厅ID为名称Cookie中
}
/************************
**加载菜的分类列表单击事件
*************************/
var classify = document.getElementById("classify");
//alert(classify.getElementsByTagName("li").length);
//alert(classify.getElementsByTagName("li")[2]);
for( i = 0; i < classify.getElementsByTagName("li").length ; i++ )
{
		classify.getElementsByTagName("li")[i].addEventListener("click",classifySelected,false);
}
function classifySelected(){
	//所有菜的分类样式清空，设置选择的当前分类样式为已选
	for( i = 0; i < classify.getElementsByTagName("li").length ; i++ )
	{ 
		classify.getElementsByTagName("li")[i].className=""; 
	}
	this.className="selected";
	//所有详细菜品列表清空，只显示当前选择的分类对应的菜品
	var dishes = document.getElementById("dishes_" + this.id);
	
	for (i = 1; i <= classify.getElementsByTagName("li").length ; i++ )
	{
		document.getElementById("menu").getElementsByTagName("section")[i].className = "goods-list hidden";
	}
	dishes.className = "goods-list visible";
	if ( window.innerWidth < 800 )
	{
		window.scrollTo(0,0);//返回顶部看菜品
		//hiddenClassify(this.innerHTML);//隐藏分类选择图层
	}
	//autoHeight();
}
//addEventListener
/************************
**加载菜品单击事件
*************************/
var menu = document.getElementById("menu");
var menuDiv = menu.getElementsByTagName("div");
var menuDivNum = menuDiv.length;

var clicked = "no" ;//用于判断是否点击过选择框
for (var i = 1 ; i < menuDivNum-2 ; i++ )//第一个和最后2个div不用于放置菜品
{	
	for(var s = 1; s<menuDiv[i].getElementsByTagName("section").length; s++)
	{
		var dishes = menuDiv[i].getElementsByTagName("section")[s].getElementsByTagName("dl");
		
		var dishesNum = dishes.length;
		
		for (var j = 0 ; j < dishesNum ; j++ )
		{
			//dishes[j].addEventListener("click",dishesSelected,false);
			dishes[j].getElementsByTagName("input")[0].addEventListener("click",dishesSelected,false);
		}
	}
}
function dishesSelected(){
	//alert("test");
	if ( !this.getElementsByTagName("input")[0] )//如果点击到的是选择框
	{
//		alert("check");
		var dishDiv = this.parentNode.parentNode.parentNode.parentNode ;
		var checkButton = this ;
		var thisDishID = this.id.split("_")[1];
		clicked = "yes" ;
		if ( checkButton.checked == true )
		{
			dishDiv.style.backgroundColor = '#E1E1E1';
			selectedDishes.push( thisDishID );
		}
		else
		{
			dishDiv.style.backgroundColor = 'transparent';
			for ( i=0 ; i < selectedDishes.length ; i++ )
			{
				if ( selectedDishes[i] == thisDishID )
				{
					selectedDishes.splice(i,1);
				}
			} 	
		}
	}
	else {//如果点击到的是整个Div
//		alert("button");
		//alert(clicked);
		if ( clicked == "yes" ){ clicked = "no"; return; }
		/**
		var dishDiv = this ;
		var checkButton = this.getElementsByTagName("input")[0];
		//alert(this.id);
		var thisDishID = this.id.split("_")[1];
		if ( checkButton.checked == false )
		{
			addDish(thisDishID);
		}
		else
		{
			delDish(thisDishID);
		}
		*/
		/*var choosePro = this.getElementsByTagName("input")[1];
		var thisDishID = this.id.split("_")[1];
		var value = choosePro.value;
		
		if ( choosePro.click && value=="选择")
		{
			choosePro.value = "取消";
			addDish(thisDishID);
		}
		else if( choosePro.click && value=="取消")
		{
			choosePro.value = "选择";
			delDish(thisDishID);
		}*/
	}
	
	//alert( selectedDishes );
	sum();
}
/*** 调整菜品容器,适应不同的内容高度 ***/
function autoHeight(){
	for ( i=1 ; i < menuDivNum-2 ; i++ )
	{
		menuDiv[i].style.top = document.getElementById("classify").offsetHeight + 85 + "px";//控制菜单与菜品分类选择图层的距离
		//alert(document.getElementById("classify").offsetHeight);

		var dishes = menuDiv[i].getElementsByTagName("section")[0].getElementsByTagName("dl");
		var dishesNum = dishes.length;
		for ( j = 0 ; j < dishesNum ; j++ )
		{
			
			//if ( dishes[j].offsetHeight > 170 || dishes[j].offsetHeight > 65 && dishes[j].offsetHeight < 135 )
			//{
				//dishes[j].style.cssFloat = "none";
				//dishes[j].style.styleFloat = "none";
				//dishes[j].style.clear = "both";
				dishes[j].style.width = "65";
				dishes[j].style.height = "152px";
				dishes[j].style.marginBottom = "0px";
				if ( !dishes[j].getElementsByTagName("img")[0] || dishes[j].getElementsByTagName("img")[0].className == "good" )
				{
					dishes[j].style.height = "61px";
					//dishes[j].style.cssFloat = "none";
					//dishes[j].style.styleFloat = "none";
					//从第一张没有配图的菜开始重新排列
					if ( j > 0 && ( dishes[j-1].getElementsByTagName("img")[0] && dishes[j-1].getElementsByTagName("img")[0].className != "good" ) )
					{
						dishes[j].style.clear = "both";
					}
					//dishes[j].getElementsByTagName("img")[0].style.cssFloat = "left";
					//dishes[j].getElementsByTagName("img")[0].style.styleFloat = "none";
					//dishes[j].getElementsByTagName("img")[0].style.marginRight = "15px";
				}
				//dishes[j+1].style.height = ( dishes[j].clientHeight - 20 ) / 2 - 15 + "px";
				//dishes[j+2].style.height = ( dishes[j].clientHeight - 20 ) / 2 - 15 + "px";

			//}			
		}
	}
}
//autoHeight();

/*** 菜品分类浏览控制 顶部与底部浮动控制 ***/
var header = document.getElementById("header");
var orderInfoA = document.getElementById("orderInfoA");
var leftClassify = document.getElementById("leftClassify");
var classify = document.getElementById("classify");
var classifyWidth = 0 ;
//根据菜品分类内容(li数量)自动设置容器(ul)宽度
for ( i=0 ; i < classify.getElementsByTagName("li").length; i++ )
{
	classifyWidth = classifyWidth + classify.getElementsByTagName("li")[i].offsetWidth + 10;
}

function showClassify(){
	var chooseICO = document.getElementById("chooseICO");
	//var chooseText = document.getElementById("chooseText");
	
	var proDiv = document.getElementById("productDiv");
	var top = proDiv.style.marginTop;
	
	if ( chooseICO.className == "chooseICO_B" )//leftClassify.style.display == "none" || leftClassify.style.display == "" || 
	{
		leftClassify.style.display = "none";
		chooseICO.className = "chooseICO_A";
		//chooseText.innerHTML = "显示菜品分类";
		
		proDiv.style.marginTop = "70px";
	}
	else
	{
		leftClassify.style.display = "inline";
		chooseICO.className = "chooseICO_B";
		//chooseText.innerHTML = "隐藏菜品分类";
		proDiv.style.marginTop = "130px";
	}
}
function hiddenClassify(text){
	leftClassify.style.display = "none";
	document.getElementById("choose").innerHTML = '<img src="images/transparent.png" id="chooseICO" class="chooseICO_A" /> ' + text;
}

//document.getElementById("loading").style.display = "none";
document.body.style.overflow = "auto";